﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.Util;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using System.IO;

namespace Com.CMS263.Web.manage.ManageSingleArt
{
    public partial class AddBaseArt : System.Web.UI.Page
    {
        public ExtAttributeBLL extbll = new ExtAttributeBLL();
        public IList<ExtAttribute> extList = null;
        public ArtTypeBLL atbll = new ArtTypeBLL();
        public int page = 1;
        public int pid;
        int menuId;
        ArtBaseBLL artBll = new ArtBaseBLL();
        Com.CMS263.PO.ArtType type = null;
        public string menuTitle = "";
        protected void Page_Load(object sender, EventArgs e)
        {


            //根据父类类型盘点是否出默认结果

            pid = int.Parse(Request["pid"]);
             menuId = int.Parse(Request["menuId"]);
            extList = extbll.GetExtAttributeByTypeId(pid);
            type = atbll.GetArtTypeById(menuId);
            page = string.IsNullOrEmpty(Request["page"]) ? 1 : int.Parse(Request["page"]);
            menuTitle = type.typeName;
            
            this.hdfTypeId.Value = pid.ToString();
            this.hdfMenuId.Value = Request["menuid"];
            if (artBll.GetArtBaseByMenuId(menuId) != null)
            {
                Response.Redirect("ModArt.aspx?pid="+pid+"&menuId="+menuId);
            }
            btnSave.Attributes.Add("onclick", "return check()");



        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ArtBaseBLL abbll = new ArtBaseBLL();
            //文件按天存储，没有目录就自己创建
            string strDay = DateTime.Now.ToString("yyyy-MM-dd");
            string savePath = Server.MapPath("/ArtUpFile/" + strDay);
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            string title = Request["title"];
            if (!string.IsNullOrEmpty(title))
            {
                if (chkStrong.Checked)
                {
                    title = "<b>" + title + "</b>";

                }
                if (this.chkRed.Checked)
                {
                    title = "<span style='color:red'>" + title + "</span>";
                }
            }
            else
            { StringUtil.Alert("请输入标题", "-1"); }
            string addDate = Request["addDate"];
           
            int typeId = int.Parse(this.hdfTypeId.Value);
            int menuId = int.Parse(this.hdfMenuId.Value);
            ArtBase ab = new ArtBase();
            ab.title = title;
            ab.addDate = DateTime.Parse(addDate);
            ab.clickCount = Convert.ToInt32(StringUtil.checkStr(Request["clickCount"]));
          
            ab.isBest = 0;
            ab.isShow = 1;
            ab.orderNum = 0;
            ab.typeId = typeId;
            ab.menuId = menuId;
            IList<ExtAttribute> attrList = extbll.GetExtAttributeByTypeId(typeId);
            IList<ExtValue> valList = new List<ExtValue>();
            //循环属性列表得到属性值并天数值列表
            foreach (ExtAttribute item in attrList)
            {
                string val = "";
                int notNull = item.notNull;
                if (item.attrType == 4 || item.attrType == 5)
                {
                    HttpPostedFile hpf = Request.Files[item.attrName];
                    if (notNull == 1)
                    {
                        if (hpf.ContentLength < 5)
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                    }
                    val = strDay + "/" + UpLoadFileUtil.UpLoadFile(Request.Files[item.attrName], savePath);
                }
                else
                {
                    if (notNull == 1)
                    {
                        if (string.IsNullOrEmpty(Request[item.attrName]))
                        {
                            StringUtil.Alert(item.attrTxtName + "是必填的", "-1");
                            return;
                        }
                    }
                    val = Request[item.attrName];
                }
                ExtValue ev = new ExtValue();
                ev.attrId = item.id;
                ev.val = val;
                valList.Add(ev);

            }
            ab.valueList = valList;

            abbll.Add(ab);
            StringUtil.Alert("添加成功", "ModArt.aspx?pid=" + pid.ToString() + "&menuId=" + menuId.ToString());


        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
